package com.my.adminservice.map.controller;

import com.my.adminapi.map.domin.dto.LocationSearchReqDTO;
import com.my.adminapi.map.domin.dto.PlaceSearchReqDTO;
import com.my.adminapi.map.domin.vo.RegionCityVO;
import com.my.adminapi.map.domin.vo.RegionVO;
import com.my.adminapi.map.domin.vo.SearchPoiVo;
import com.my.adminapi.map.feign.MapFeignClient;
import com.my.adminservice.map.domain.dto.RegionCityDTO;
import com.my.adminservice.map.domain.dto.SearchPoiDTO;
import com.my.adminservice.map.domain.dto.SysRegionDTO;

import com.my.adminservice.map.service.IMapService;
import com.my.mycommoncore.domain.dto.BasePageDTO;
import com.my.mycommoncore.utils.BeanCopyUtils;
import com.my.mycommondomain.domian.R;
import com.my.mycommondomain.domian.vo.BasePageVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

@Slf4j
@RestController
public class MapController implements MapFeignClient {


    @Autowired
    private IMapService mapService;
//    @Autowired
//    private RegionMapper regionMapper;

    /**
     * 城市列表查询
     * @return  城市列表
     */
    @Override
    public R<List<RegionVO>> getCityList() {
        List<SysRegionDTO> cityList = mapService.getCityList();
        List<RegionVO> regionVOS = BeanCopyUtils.copyList(cityList,RegionVO::new);
        return R.ok(regionVOS);
    }

    @Override
    public R<Map<String, List<RegionVO>>> getRegionPinyinList() {
        Map<String,List<RegionVO>> map=new TreeMap<>();
        Map<String, List<SysRegionDTO>> cityListPinyin = mapService.getCityListPinyin();
        for(Map.Entry<String,List<SysRegionDTO>> region:cityListPinyin.entrySet()){
            map.put(region.getKey(),BeanCopyUtils.copyList(region.getValue(),RegionVO::new));
        }
        return R.ok(map);
    }

    @Override
    public R<List<RegionVO>> ChildrenRegionList(Long parentId) {
        List<SysRegionDTO> childrenList = mapService.getChildrenList(parentId);
        List<RegionVO> regionVOS = BeanCopyUtils.copyList(childrenList, RegionVO::new);
        return R.ok(regionVOS);
    }

    @Override
    public R<List<RegionVO>> cityHotList() {
        List<SysRegionDTO> cityList = mapService.getHostCityList();
        List<RegionVO> regionVOS = BeanCopyUtils.copyList(cityList, RegionVO::new);
        return R.ok(regionVOS);
    }

    @Override
    public R<BasePageVO<SearchPoiVo>> searchSuggestOnMap(PlaceSearchReqDTO placeSearchReqDTO) {
        BasePageDTO<SearchPoiDTO> result=mapService.getSearchMapList(placeSearchReqDTO);
        //结果转换
        BasePageVO<SearchPoiVo> basePageVO= new BasePageVO<>();
        BeanCopyUtils.copyProperties(result,basePageVO);
        return R.ok(basePageVO);
    }

    /**
     * 根据经纬度查询城市信息
     * @param locationSearchReqDTO 入参
     * @return 城市
     */
    @Override
    public R<RegionCityVO> locateCityByLocation(LocationSearchReqDTO locationSearchReqDTO) {
        RegionCityVO regionCityVO=new RegionCityVO();
        RegionCityDTO result=mapService.getCityByLocation(locationSearchReqDTO);
        BeanCopyUtils.copyProperties(result,regionCityVO);
        return R.ok(regionCityVO);
    }
}
